package com.lottery.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

public class DateUtils {


    public static final long ONE_DAY_MILLIS = 24 * 60 * 60 * 1000L;//一天毫秒数


    /**
     * yyyy-mm-dd
     */
    public static String getBeforeDate(int before) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        c.add(Calendar.DATE, -before);
        Date monday = c.getTime();
        return sdf.format(monday);
    }

    public static String nowDatetime() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(new Date());
    }

    public static String nowTime() {
        return getSub4DT().substring(11, 19);
//        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
//        return sdf.format(new Date());
    }

    public static String nowDate() {
        return getSub4Date();
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//        return sdf.format(new Date());
    }

    /*星期几*/
    public static int getWeekOfDate(Date dt) {
        int[] weekDays = {7, 1, 2, 3, 4, 5, 6};
        Calendar cal = Calendar.getInstance();
        cal.setTime(dt);
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0)
            w = 0;
        return weekDays[w];
    }

    /*判断当前时间在时间区间内*/
    public static boolean isEffectiveDate(Date nowTime, Date startTime, Date endTime) {
        if (nowTime.getTime() == startTime.getTime()
                || nowTime.getTime() == endTime.getTime()) {
            return true;
        }

        Calendar date = Calendar.getInstance();
        date.setTime(nowTime);

        Calendar begin = Calendar.getInstance();
        begin.setTime(startTime);

        Calendar end = Calendar.getInstance();
        end.setTime(endTime);

        return date.after(begin) && date.before(end);
    }

    /*获取西四区时间*/
    public static String getSub4DT() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        format.setTimeZone(TimeZone.getTimeZone("GMT-4:00"));
        return format.format(new Date());
    }

    public static String getSub4Date() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        format.setTimeZone(TimeZone.getTimeZone("GMT-4:00"));
        return format.format(new Date());
    }

    /*减若干分钟*/
    public static String subDTHours4(String dt) {
        return subDTHours(dt, 4);
    }

    public static String subDTHours(String dt, int hours) {
        if (dt == null || dt.trim().equals("")) {
            return "";
        }

        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date = format.parse(dt);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(Calendar.HOUR, -hours);
            return format.format(calendar.getTime());
        } catch (Exception e) {
            return "";
        }
    }

    public static void main(String[] args) {
        System.out.println(getWeekOfDate(new Date()));
//        System.out.println(subDTHours("2022-04-01 14:21:00", 4));
//        System.out.println(subDTHours4("2022-04-01 16:21:50"));
    }
}
